package models

import (
	"GoTime/response"
	"time"

	"xorm.io/builder"
)

type Reported struct {
	Id                int       `xorm:"not null pk autoincr INT(11)"`
	StudentId         int       `xorm:"index INT(11)"`
	SchoolId          int       `xorm:"index INT(11)"`
	Subject           string    `xorm:"comment('科目') index ENUM('1','2','3','4')"`
	Validetime        int64     `xorm:"INT(11)"`
	Result            string    `xorm:"comment('0不合格1合格2审核中') ENUM('0','1','2')"`
	Needtime          int64     `xorm:"default 0 comment('需要补充的学时') INT(11)"`
	Needmileage       float64   `xorm:"default 0 comment('需要补学公里数') FLOAT"`
	Reason            string    `xorm:"default '' comment('不合格的原因') VARCHAR(255)"`
	Checktime         time.Time `xorm:"index DATETIME"`
	Validmileage      float32   `xorm:"default 0 comment('有效公里数') FLOAT"`
	Isreport          string    `xorm:"comment('0未上报1已上报') ENUM('0','1')"`
	PdfId             int64     `xorm:"comment('电子培训部分记录表PDF文件ID') index INT(11)"`
	CheckStatus       string    `xorm:"comment('0不合格1合格') ENUM('0','1')"`
	RecheckStatus     string    `xorm:"comment('0不合格1合格') ENUM('0','1')"`
	NeedNetworktime   int       `xorm:"default 0 comment('需要远程的学时') INT(11)"`
	NeedClasstime     int       `xorm:"default 0 comment('需要课堂的学时') INT(11)"`
	NeedVehicletime   int       `xorm:"default 0 comment('需要实车的学时') INT(11)"`
	NeedSimulatortime int       `xorm:"default 0 comment('需要模拟器的学时') INT(11)"`
	UpdateTime        time.Time `xorm:"index DATETIME"`
	Duration          int64     `xorm:"not null comment('审核合格学时') INT(11)"`
	TotalInvalidTime  int64     `xorm:"not null comment('本培训部分本次的审核不合格总学时，n分钟') INT(11)"`
	Sign              string    `xorm:"default '' comment('审核员签字') VARCHAR(255)"`
}

func (r *Reported) StageRecord(school_id string, filter builder.Cond, pagesize, offset int) (list []*response.StageListResponse, err error, count int64) {
	err = engine.Table("dts_reported").Alias("t").
		Select("t.id,ur.name as student_name,t.pdf_id,ur.phone,ur.id_number,t.sign,t.result,t.check_status as chushen,st.operation_status,t.recheck_status as fushen,t.subject,st.training_car_type as car_type,t.needtime,t.needmileage,t.isreport,st.id as student_id,t.need_simulatortime,t.need_vehicletime,t.need_networktime,t.need_classtime,t.checktime,t.validetime,t.validmileage,st.sign_in_time").
		Join("left", []string{"dts_student", "st"}, "t.student_id = st.id").
		Join("left", []string{"dts_user", "ur"}, "st.user_id = ur.id").
		Where("t.school_id = ? and st.isused = ?", school_id, "1").
		And(filter).
		Desc("t.id").
		Limit(pagesize, offset).
		Find(&list)
	count, _ = engine.Table("dts_reported").Alias("t").
		Join("left", []string{"dts_student", "st"}, "t.student_id = st.id").
		Join("left", []string{"dts_user", "ur"}, "st.user_id = ur.id").
		Where("t.school_id = ? and st.isused = ?", school_id, "1").
		And(filter).
		Count()
	return list, err, count
}
